<?php
include("include/config.inc.php");
  if(!isset($_SESSION['s_activId']))
  {
    $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
    header("Location:checkLogin.php");
  }
 
  if(isset($_REQUEST['waiter']))
  {
    $waiterVar = $_REQUEST['waiter'] ;
  }
  else
  {
  	$waiterVar = 0 ;
  }
  
  if(isset($_REQUEST['table']))
  {
    $tableVar = $_REQUEST['table'] ;
  }
  else
  {
  	$tableVar = 0 ;
  }
  if(isset($_REQUEST['tablePart']))
  {
    $tablePartVar = $_REQUEST['tablePart'] ;
  }
  else
  {
  	$tablePartVar = "A" ;
  }
  

$orderstatus = "";
$joinResult = array();
$i=0;
$allTablesSelected = isset($_POST['allTables']) ? $_POST['allTables'] : 0;
$joinOrderMasterOrderedItems = " SELECT waiter.waiterName, waiter.waiterId, ordermaster.tablePart,ordermaster.orderId,
                                        ordermaster.tableId, ordermaster.orderstatus, 
                                        SUM( ordereditems.quantity * item.itemPrice ) AS totalamount, 
                                        COUNT( * ) AS totalItem, ordermaster.startTime, 
                                        MAX( ordereditems.orderTime ) AS lastordertime
                                   FROM ordermaster
                                   JOIN ordereditems ON ordermaster.orderId = ordereditems.orderId
                                   JOIN item ON item.itemId = ordereditems.itemId
                                   JOIN waiter ON waiter.waiterId = ordermaster.waiterId
                                  WHERE 1 = 1";
if(isset($_POST['allTables']) && $_POST['allTables'] == 'Closed Tables')
{
  $joinOrderMasterOrderedItems .= " AND ordermaster.orderstatus = 'C'";
}
else if(isset($_POST['allTables']) && $_POST['allTables'] == 'Live Tables')
{
	$joinOrderMasterOrderedItems .= " AND ordermaster.orderstatus = 'L'";
}
else if(isset($_POST['allTables']) && $_POST['allTables'] == 'Receive Tables')
{
	$joinOrderMasterOrderedItems .= " AND ordermaster.orderstatus = 'R'";
}
$joinOrderMasterOrderedItems .= " GROUP BY ordermaster.tableId";
$joinOrderMasterOrderedItemsRes = mysql_query($joinOrderMasterOrderedItems);
while($joinOrderMasterOrderedItemsRow = mysql_fetch_array($joinOrderMasterOrderedItemsRes))
{
	 $joinResult[$i]['orderId']       = $joinOrderMasterOrderedItemsRow['orderId'];
	 $joinResult[$i]['waiterName']    = $joinOrderMasterOrderedItemsRow['waiterName'];
	 $joinResult[$i]['waiterId']      = $joinOrderMasterOrderedItemsRow['waiterId'];
	 $joinResult[$i]['tableId']       = $joinOrderMasterOrderedItemsRow['tableId'];
	 $joinResult[$i]['tablePart']     = $joinOrderMasterOrderedItemsRow['tablePart'];
	 $joinResult[$i]['totalamount']   = $joinOrderMasterOrderedItemsRow['totalamount'];
	 $joinResult[$i]['totalItem']     = $joinOrderMasterOrderedItemsRow['totalItem'];
	 $joinResult[$i]['startTime']     = $joinOrderMasterOrderedItemsRow['startTime'];
	 $joinResult[$i]['lastordertime'] = $joinOrderMasterOrderedItemsRow['lastordertime'];
	 $joinResult[$i]['tablePart']     = $joinOrderMasterOrderedItemsRow['tablePart'];
	 $joinResult[$i]['orderstatus']   = $joinOrderMasterOrderedItemsRow['orderstatus'];
	 $i++;
}
$tableStausValueArr[0]  = "Closed Tables";
$tableStausOutputArr[0] = "Closed Tables";
$tableStausValueArr[1]  = "Live Tables";
$tableStausOutputArr[1] = "Live Tables";
$tableStausValueArr[2]  = "Receive Tables";
$tableStausOutputArr[2] = "Receive Tables";

include("./bottom.php");
 $smarty->assign('tableVar',$tableVar);
 $smarty->assign('tablePartVar',$tablePartVar);
 $smarty->assign('waiterVar',$waiterVar);
$smarty->assign('joinResult',$joinResult);
$smarty->assign('allTablesSelected',$allTablesSelected);
$smarty->assign('tableStausValueArr',$tableStausValueArr);
$smarty->assign('tableStausOutputArr',$tableStausOutputArr);
$smarty->assign('orderstatus',$orderstatus);
$smarty->display('closeTables.tpl');
?>